package com.Xingcang_Dianxue.WhutBank.Views;

import com.Xingcang_Dianxue.WhutBank.Controllers.Client.ClientController;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class ViewFactory {
    private final ObjectProperty<ClientMenuOptions> clientSelectedMenuItem;
    private AnchorPane dashboardView;
    private AnchorPane saveView;
    private AnchorPane withdrawalView;
    private AnchorPane transactionsListView;
    private AnchorPane profileView;
    private Dialog showMessageDialog;

    public ViewFactory() {
        this.clientSelectedMenuItem = new SimpleObjectProperty<>();
    }



    public ObjectProperty<ClientMenuOptions> getClientSelectedMenuItem() {
        return clientSelectedMenuItem;
    }


    // Client Views Section

    /**
     * 获取首页视图
     *
     * @return 返回加载的Dashboard.fxml界面
     */
    public AnchorPane getDashboardView() {
        if (dashboardView == null) {
            try {
                // 使用 FXMLLoader 加载 Dashboard.fxml 文件，并加载到 dashboardView 变量中
                dashboardView = new FXMLLoader(getClass().getResource("/Fxml/Client/Dashboard.fxml")).load();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dashboardView;
    }

    /**
     * 获取存款视图界面
     *
     * @return 返回加载的Save.fxml界面
     */
    public AnchorPane getSaveViewView() {
        if (saveView == null) {
            try {
                saveView = new FXMLLoader(getClass().getResource("/Fxml/Client/Save.fxml")).load();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return saveView;
    }

    /**
     * 获取取款视图
     *
     * @return 返回加载的Withdrawal.fxml界面
     */
    public AnchorPane getWithdrawalView() {
        if (withdrawalView == null) {
            try {
                withdrawalView = new FXMLLoader(getClass().getResource("/Fxml/Client/Withdrawal.fxml")).load();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return withdrawalView;
    }

    /**
     * 获取交易记录视图
     *
     * @return 返回加载的TransactionsList.fxml界面
     */
    public AnchorPane getTransactionsListView() {
        if (profileView == null) {
            try {
                profileView = new FXMLLoader(getClass().getResource("/Fxml/Client/TransactionsList.fxml")).load();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return profileView;
    }

    /**
     * 获取个人中心视图
     *
     * @return 返回加载的Profile.fxml界面
     */
    public AnchorPane getProfileView() {
        if (transactionsListView == null) {
            try {
                transactionsListView = new FXMLLoader(getClass().getResource("/Fxml/Client/Profile.fxml")).load();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return transactionsListView;
    }



    /**
     * 显示登录窗口
     */
    public void showLoginWindow() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Fxml/Login.fxml"));
        createStage(fxmlLoader);
    }

    /**
     * 显示客户端窗口
     */
    public void showClientWindow() {
        // 使用 FXMLLoader 加载 Client.fxml 格式的 FXML 文件
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Fxml/Client/Client.fxml"));
        // 创建 ClientController 对象作为控制器
        ClientController clientController = new ClientController();
        // 设置 FXMLLoader 的控制器为新创建的 ClientController 对象
        fxmlLoader.setController(clientController);
        // 调用 createStage 方法创建一个新的 Stage，并使用 FXMLLoader 设置 FXML 文件的根节点
        createStage(fxmlLoader);
    }

    /**
     * 创建舞台方法：创建场景对象和舞台对象，并设置舞台的场景、大小、标题、图标以及显示舞台。
     *
     * @param loader 加载FXML文件的工具类
     */
    private void createStage(FXMLLoader loader) {
        // 创建场景对象
        Scene scene = null;
        try {
            scene = new Scene(loader.load());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建舞台对象
        Stage stage = new Stage();
        // 设置舞台的场景
        stage.setScene(scene);
        // 设置舞台不可调整大小
        stage.setResizable(false);
        // 设置舞台的标题
        stage.setTitle("WhutBank");
        // 设置舞台的图标
        stage.getIcons().add(new Image("/Images/icon.png"));
        // 显示舞台
        stage.show();
    }

    /**
     * 关闭场景
     *
     * @param stage 需要关闭的场景
     */
    public void closeScene(Stage stage) {
        stage.close();
    }

}
